package cn.itcast.cart.service.impl;

import cn.itcast.cart.dto.ShoppingCartDTO;
import cn.itcast.cart.mapper.ShoppingCartMapper;
import cn.itcast.cart.pojo.ShoppingCart;
import cn.itcast.cart.service.ShoppingCartService;
import cn.itcast.context.BaseContext;
import cn.itcast.goods.feign.DishFegin;
import cn.itcast.goods.feign.SetmealFeign;
import cn.itcast.goods.pojo.Dish;
import cn.itcast.goods.vo.SetmealVO;
import cn.itcast.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
//    @Autowired
//    private DishService dishService;
//    @Autowired
//    private SetmealService setmealService;
    @Autowired
    private DishFegin dishFegin;
    @Autowired
    private SetmealFeign setmealFeign;

    /**
     * 根据购物车，查询购物车列表
     *
     * @param shoppingCart
     * @return
     */
    @Override
    public List<ShoppingCart> search(ShoppingCart shoppingCart) {
        return shoppingCartMapper.list(shoppingCart);
    }

    /**
     * 根据用户id删除购物车
     *
     * @param userId
     */
    @Override
    public void cleanShoppingCart(Long userId) {
        shoppingCartMapper.cleanShoppingCart(userId);
    }

    /**
     * 添加购物车
     *
     * @param dto
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO dto) {
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .dishId(dto.getDishId())
                .setmealId(dto.getSetmealId())
                .dishFlavor(dto.getDishFlavor())
                .userId(BaseContext.getCurrentId())
                .build();
        //查询出购物车信息
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);


        //如果没有信息则进行添加操作，如果有信息则进行修改操作(修改其实也就是修改数量)
        if( shoppingCartList.size() == 0 ){
            //如果要添加的是菜品
            if( shoppingCart.getDishId() != null ){
//                Dish dish = dishService.getById(shoppingCart.getDishId());
                Long dishId = shoppingCart.getDishId();
                Result<Dish> dishByDishId = dishFegin.getDishByDishId(dishId);
                Dish dish =  dishByDishId.getData();
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }
            //如果要添加的是套餐
            if( shoppingCart.getSetmealId() != null ){
//                SetmealVO setmealVO = setmealService.getById(shoppingCart.getSetmealId().intValue());
                Result<SetmealVO> re = setmealFeign.getById(shoppingCart.getSetmealId().intValue());
                SetmealVO setmealVO = re.getData();
                shoppingCart.setName(setmealVO.getName());
                shoppingCart.setImage(setmealVO.getImage());
                shoppingCart.setAmount(setmealVO.getPrice());
            }
            //添加公共字段
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);

            //开始添加
            shoppingCartMapper.add(shoppingCart);
        }else {
            //如果有数据则进行修改操作
            ShoppingCart cart = shoppingCartList.get(0);
            cart.setNumber(cart.getNumber()+1);
            shoppingCartMapper.update(cart);
        }
    }

    /**
     * 查看购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .build();
        return shoppingCartMapper.list(shoppingCart);
    }

    /**
     * 删除购物车中的一个商品
     *
     * @param dto
     */
    @Override
    public void delete(ShoppingCartDTO dto) {
        //删除操作，先判断这个数据有多少个，如果数量大于1则将商品数量减一
        //如果这个商品只剩下一个，则在数据库中删除这条数据
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .dishId(dto.getDishId())
                .setmealId(dto.getSetmealId())
                .dishFlavor(dto.getDishFlavor())
                .userId(BaseContext.getCurrentId())
                .build();
        shoppingCart = shoppingCartMapper.list(shoppingCart).get(0);

        if( shoppingCart.getNumber() > 1){
            //进行修改操作，将数据的数量减一
            shoppingCart.setNumber(shoppingCart.getNumber()-1);
            shoppingCartMapper.update(shoppingCart);
        }else {
            //进行删除操作，将数据从数据库中删除
            shoppingCartMapper.deleteById(shoppingCart.getId());
        }
    }

    /**
     * 清空购物车
     */
    @Override
    public void cleanShoppingCart() {
        shoppingCartMapper.cleanShoppingCart(BaseContext.getCurrentId());
        return;
    }


}